<template>
  <section style="background-color: #eee;padding: 5px;border-radius: 5px">
    <el-row>
      <el-input size="mini" v-model="searchKey" style="width: 80%" placeholder="输入关键字筛选" @keydown.native.prevent.enter="searchBtn(item.contentType)"></el-input>
      <i class="el-icon-search"  @click="searchBtn(item.contentType)"></i>
    </el-row>
    <el-table :data="list" min-height="200" >
      <el-table-column type="index" label="序号"/>
      <el-table-column label="名称">
        <template slot-scope="scope">
          {{scope.row.title?scope.row.title:scope.row.name}}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="checkContentVal(scope.row)">check</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-show="totalPage>0" style="margin-top:20px;"
                   layout="prev, pager, next" :page-count="totalPage"
                   @current-change="goPage($event,item.contentType)"/>
  </section>
</template>

<script>
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import Vue from 'vue'//后续添加属性，需要vue联动
  import errHandler from '@/assets/errHandler'
  export default {
    name: "selContentVal",
    props:['item','departmentId'],
    data(){
      return {
        list:null,
        totalPage:0,
        searchKey:null
      }
    },
    watch:{
      item(nv,ov){
        console.log('item--watch----',nv)
      }
    },
    created(){
      console.log('selVal-----',this.item)
      this.getList(this.item.contentType)
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      goPage(val,code){
        if(code === 2){
          this.getGoodsList(val-1)
        }else if(code === 1){
          this.getActivities(val-1)
        }
      },
      searchBtn(code){
        if(code === 2){
          this.getGoodsList()
        }else if(code === 1){
          this.getActivities()
        }
      },
      checkContentVal(row){
        this.item.contentValue=row.id
        this.$emit('selVal',row.id)
      },
      getList(code){
        if(code === 2){
          this.getGoodsList()
        }else if(code === 1){
          this.getActivities()
        }else if(code === 3){
          this.list=[]
          let keyList=[
            {key:'goodsTypeId',name:'商品类型',val:null},
            {key:'goodsTypeCode',name:'商品类型编码',val:null},
            {key:'departmentId',name:'部门',val:null},
            {key:'departmentCode',name:'部门编码',val:null},
            {key:'key',name:'关键字',val:null}
          ]
          this.list=this.list.concat(keyList)
        }else if(code === 4){
          this.getStoreList()
        }
      },
      getGoodsList(pageNo){
        this.fetch({name:"getMallGoodsList",data:{page:pageNo?pageNo:0,key:this.searchKey,storeId:this.departmentId}})
          .then(res=>{
            console.log(res)
            this.list=res.content
            this.totalPage=res.totalPages
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      getStoreList(){
        this.fetch({name:"getStoreList"})
          .then(res=>{
            console.log(res)
            this.list=res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      getActivities(pageNo){
        this.fetch({name:"getActivityListByAdmin",data:{page:pageNo?pageNo:0,departmentId:this.departmentId,key:this.searchKey}})
          .then(res=>{
            console.log(res)
            this.list=res.content
            this.totalPage=res.totalPages
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
    }
  }
</script>

<style scoped>

</style>
